﻿using QuanLyChoVayCaNhan.Classes;
using QuanLyChoVayCaNhan.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyChoVayCaNhan.Forms
{
    public partial class MainForm : Form
    {
        private List<TabType> Tabs = new List<TabType>();
        private BaseTab ActiveTab = null;
        public MainForm()
        {
            InitializeComponent();
            mainTabs.TabPages.Remove(tabNhanVienPage);
            mainTabs.TabPages.Remove(tabKhachHangPage);
            mainTabs.TabPages.Remove(tabTiepNhanHoSoPage);
            mainTabs.TabPages.Remove(tabThamDinhTinDungPage);
            mainTabs.TabPages.Remove(tabThamDinhTaiSanDamBaoPage);
            mainTabs.SelectedIndex = -1;
        }

        private void menuThoat_Click(object sender, EventArgs e)
        {
            Activity.GetInstance().Exit();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát chương trình", "Xác nhận thoát", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Activity.GetInstance().Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void menuDangXuat_Click(object sender, EventArgs e)
        {
            Activity.GetInstance().Logout();
        }

        private void SubmenuButton_Click(object sender, EventArgs e)
        {
            ToolStripButton button = (ToolStripButton)sender;
            MenuItemType mType = (MenuItemType)button.Tag;
            if (ActiveTab != null) ActiveTab.MenuClick(mType);
        }



        #region Tabs

        private bool IsContain(TabType tabType)
        {
            foreach (var item in Tabs)
            {
                if (item == tabType) return true;
            }
            return false;
        }
        private void ChangeActiveTab(BaseTab tab)
        {
            ActiveTab = tab;
            switch (tab.GetTabType())
            {
                case TabType.NhanVienTab:
                    mainTabs.SelectedTab = tabNhanVienPage;
                    break;
                case TabType.KhachHangTab:
                    mainTabs.SelectedTab = tabKhachHangPage;
                    break;
                case TabType.TiepNhanHoSoTab:
                    mainTabs.SelectedTab = tabTiepNhanHoSoPage;
                    break;
                case TabType.ThamDinhTinDungTab:
                    mainTabs.SelectedTab = tabThamDinhTinDungPage;
                    break;
                case TabType.ThamDinhTaiSanDamBaoTab:
                    mainTabs.SelectedTab = tabThamDinhTaiSanDamBaoPage;
                    break;
                default:
                    break;
            }
            rebuildSubMenu(ActiveTab.GetSupportedMenus());
            lblTabTitle.Text = ActiveTab.GetTabTitle();
        }

        private void mainTabs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mainTabs.SelectedIndex != -1)
            {
                TabType tabType = Tabs.ElementAt(mainTabs.SelectedIndex);
                if (tabType != ActiveTab.GetTabType())
                {
                    switch (tabType)
                    {
                        case TabType.NhanVienTab:
                            ActiveTab = WindowsManager.GetInstance().TabNhanVien;
                            break;
                        case TabType.KhachHangTab:
                            ActiveTab = WindowsManager.GetInstance().TabKhachHang;
                            break;
                        case TabType.TiepNhanHoSoTab:
                            ActiveTab = WindowsManager.GetInstance().TabTiepNhanHoSo;
                            break;
                        case TabType.ThamDinhTinDungTab:
                            ActiveTab = WindowsManager.GetInstance().TabThamDinhTinDung;
                            break;
                        case TabType.ThamDinhTaiSanDamBaoTab:
                            ActiveTab = WindowsManager.GetInstance().TabThamDinhTaiSanDamBao;
                            break;
                        default:
                            break;
                    }
                    rebuildSubMenu(ActiveTab.GetSupportedMenus());
                    lblTabTitle.Text = ActiveTab.GetTabTitle();
                }
            }
        }

        private System.Windows.Forms.ToolStripTextBox searchTextBox;
        private System.Windows.Forms.ToolStripComboBox searchForCombobox;
        private void rebuildSubMenu(MenuItemType[] menuItems)
        {
            subMenu.Items.Clear();
            if (menuItems.Length == 0) subMenu.Visible = false;
            foreach (MenuItemType item in menuItems)
            {
                if (item == MenuItemType.SEARCH_KHACH_HANG)
                {
                    System.Windows.Forms.ToolStripButton searchButton;
                    System.Windows.Forms.ToolStripLabel searchForLabel;
                    System.Windows.Forms.ToolStripSeparator searchSpaceter;
                    searchTextBox = new System.Windows.Forms.ToolStripTextBox();
                    searchButton = new System.Windows.Forms.ToolStripButton();
                    searchForCombobox = new System.Windows.Forms.ToolStripComboBox();
                    searchForLabel = new System.Windows.Forms.ToolStripLabel();
                    searchSpaceter = new System.Windows.Forms.ToolStripSeparator();
                    // 
                    // searchTextBox
                    // 
                    searchTextBox.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                    searchTextBox.Name = "searchTextBox";
                    searchTextBox.Size = new System.Drawing.Size(100, 25);
                    // 
                    // searchButton
                    // 
                    searchButton.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                    searchButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                    searchButton.Image = global::QuanLyChoVayCaNhan.Properties.Resources.search;
                    searchButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                    searchButton.Name = "searchButton";
                    searchButton.Size = new System.Drawing.Size(23, 22);
                    searchButton.Text = "toolStripButton1";
                    // 
                    // searchForCombobox
                    // 
            //        searchForCombobox.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            //        searchForCombobox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            //        searchForCombobox.Items.AddRange(new object[] {
            //"Số CMND",
            //"Tên Khách Hàng"});
            //        searchForCombobox.Name = "searchForCombobox";
            //        searchForCombobox.Size = new System.Drawing.Size(121, 25);
            //        searchForCombobox.SelectedIndex = 0;
                    // 
                    // searchForLabel
                    // 
                    searchForLabel.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                    searchForLabel.Name = "searchForLabel";
                    searchForLabel.Size = new System.Drawing.Size(87, 22);
                    //searchForLabel.Text = "Tìm kiếm theo:";
                    searchForLabel.Text = "Tìm kiếm theo số CMND:";
                    // 
                    // searchSpaceter
                    // 
                    searchSpaceter.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                    searchSpaceter.Name = "searchSpaceter";
                    searchSpaceter.Size = new System.Drawing.Size(6, 25);
                    //
                    // event
                    //
                    searchButton.Click += searchKhachHang_Click;
                    searchTextBox.TextChanged += searchKhachHang_Click;
                    this.subMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            searchButton,
            searchTextBox,
            /*searchForCombobox,*/
            searchForLabel,
            searchSpaceter});
                }
                else
                {
                    ToolStripButton button = new ToolStripButton();
                    //button.Image = ((System.Drawing.Image)(.GetObject("btnThem.Image")));
                    button.ImageTransparentColor = System.Drawing.Color.Magenta;
                    //this.btnThem.Size = new System.Drawing.Size(58, 22);
                    switch (item)
                    {
                        case MenuItemType.ADD:
                            button.Text = "Thêm";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("add");
                            break;
                        case MenuItemType.VIEW:
                            button.Text = "Xem";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("search");
                            break;
                        case MenuItemType.EDIT:
                            button.Text = "Sửa";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("edit");
                            break;
                        case MenuItemType.DELETE:
                            button.Text = "Xóa";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("delete");
                            break;
                        case MenuItemType.REFRESH:
                            button.Text = "Nạp";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("refresh");
                            break;
                        case MenuItemType.HELP:
                            button.Text = "Trợ giúp";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("help");
                            break;
                        case MenuItemType.PRINT:
                            button.Text = "In";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("add");
                            break;
                        case MenuItemType.VIEW_TDTD:
                            button.Text = "Xem BC thẩm định";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("search");
                            break;
                        case MenuItemType.VIEW_TTTD:
                            button.Text = "Xem tờ trình TD";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("search");
                            break;
                        case MenuItemType.VIEW_TSDB:
                            button.Text = "Xem Tài sản đảm bảo";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("search");
                            break;
                        case MenuItemType.EDIT_TDTD:
                            button.Text = "Lập BCTD";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("edit");
                            break;
                        case MenuItemType.EDIT_TTTD:
                            button.Text = "Lập TTTD";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("edit");
                            break;
                        case MenuItemType.ADD_TSDB:
                            button.Text = "Lập BBDG TSDB";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("edit");
                            break;
                        case MenuItemType.PRINT_TDTD:
                            button.Text = "In BC TDTD";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("add");
                            break;
                        case MenuItemType.PRINT_TTTD:
                            button.Text = "In TTTD";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("add");
                            break;
                        case MenuItemType.PRINT_TSDB:
                            button.Text = "In BBDG TSDB";
                            button.Image = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("add");
                            break;
                        default:
                            break;
                    }
                    button.Tag = item;
                    button.Click += SubmenuButton_Click;
                    subMenu.Items.Add(button);
                }
            }

        }

        #endregion
        #region Nhan Vien
        private void menuNhanVien_Click(object sender, EventArgs e)
        {
            if (!UserManager.GetInstance().IsAdmin())
            {
                MessageBox.Show("Bạn không có quyền thực hiện chức năng này");
                return;
            }
            if (!IsContain(TabType.NhanVienTab)) CreateNewNhanVienTab();
            ChangeActiveTab(WindowsManager.GetInstance().TabNhanVien);
        }
        private void CreateNewNhanVienTab()
        {
            if (WindowsManager.GetInstance().TabNhanVien == null) WindowsManager.GetInstance().TabNhanVien = new NhanVienTab(nhanVienBindingSource);
            BaseTab tNhanVien = WindowsManager.GetInstance().TabNhanVien;
            mainTabs.TabPages.Add(tabNhanVienPage);
            Tabs.Add(TabType.NhanVienTab);
            tNhanVien.LoadData();
            if (this.tabNhanVienData.SelectedCells.Count > 0) WindowsManager.GetInstance().TabNhanVien.SelectedIndex = this.tabNhanVienData.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabNhanVien.SelectedIndex = -1;
        }
        private void tabNhanVienData_Click(object sender, EventArgs e)
        {
            if (this.tabNhanVienData.SelectedCells.Count > 0) WindowsManager.GetInstance().TabNhanVien.SelectedIndex = this.tabNhanVienData.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabNhanVien.SelectedIndex = -1;
        }
        #endregion
        #region Khach Hang
        private void menuKhachHang_Click(object sender, EventArgs e)
        {
            if (!IsContain(TabType.KhachHangTab)) CreateNewKhachHangTab();
            ChangeActiveTab(WindowsManager.GetInstance().TabKhachHang);
        }

        private void CreateNewKhachHangTab()
        {
            if (WindowsManager.GetInstance().TabKhachHang == null) WindowsManager.GetInstance().TabKhachHang = new KhachHangTab(khachHangBindingSource);
            BaseTab tKhachHang = WindowsManager.GetInstance().TabKhachHang;
            mainTabs.TabPages.Add(tabKhachHangPage);
            Tabs.Add(TabType.KhachHangTab);
            tKhachHang.LoadData();
            if (this.tabKhachHangData.SelectedCells.Count > 0) WindowsManager.GetInstance().TabKhachHang.SelectedIndex = this.tabKhachHangData.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabKhachHang.SelectedIndex = -1;
        }
        private void tabKhachHangData_Click(object sender, EventArgs e)
        {
            if (this.tabKhachHangData.SelectedCells.Count > 0) WindowsManager.GetInstance().TabKhachHang.SelectedIndex = this.tabKhachHangData.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabKhachHang.SelectedIndex = -1;
        }
        private void searchKhachHang_Click(object sender, EventArgs e)
        {
            WindowsManager.GetInstance().TabKhachHang.FilterIndex = BaseTab.FILTER_INDEX_SEARCH_KH_BY_CMND;
            WindowsManager.GetInstance().TabKhachHang.FilterKey = this.searchTextBox.Text;
            WindowsManager.GetInstance().TabKhachHang.OnSearchKHClick();
        }
        #endregion
        #region Tiep Nhan Ho So

        private void menuHoSoMoi_Click(object sender, EventArgs e)
        {
            if (!IsContain(TabType.TiepNhanHoSoTab)) CreateNewTiepNhanHoSoTab();
            ChangeActiveTab(WindowsManager.GetInstance().TabTiepNhanHoSo);
            WindowsManager.GetInstance().TabTiepNhanHoSo.OnAddClick();
        }
        private void menuTatCaHoSo_Click(object sender, EventArgs e)
        {
            if (!IsContain(TabType.TiepNhanHoSoTab)) CreateNewTiepNhanHoSoTab();
            ChangeActiveTab(WindowsManager.GetInstance().TabTiepNhanHoSo);
        }
        private void CreateNewTiepNhanHoSoTab()
        {
            if (WindowsManager.GetInstance().TabTiepNhanHoSo == null) WindowsManager.GetInstance().TabTiepNhanHoSo = new TiepNhanHoSoTab(hoSoBindingSource);
            BaseTab tTiepNhanHoSo = WindowsManager.GetInstance().TabTiepNhanHoSo;
            mainTabs.TabPages.Add(tabTiepNhanHoSoPage);
            Tabs.Add(TabType.TiepNhanHoSoTab);
            tTiepNhanHoSo.LoadData();
            if (this.tabTiepNhanHoSoData.SelectedCells.Count > 0) WindowsManager.GetInstance().TabTiepNhanHoSo.SelectedIndex = this.tabTiepNhanHoSoData.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabTiepNhanHoSo.SelectedIndex = -1;
        }
        private void tabTiepNhanHoSoData_Click(object sender, EventArgs e)
        {
            if (this.tabTiepNhanHoSoData.SelectedCells.Count > 0) WindowsManager.GetInstance().TabTiepNhanHoSo.SelectedIndex = this.tabTiepNhanHoSoData.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabTiepNhanHoSo.SelectedIndex = -1;
        }
        #endregion
        #region Tham Dinh Tin Dung

        private void menuTDTDTatCaHoSo_Click(object sender, EventArgs e)
        {
            if (!IsContain(TabType.ThamDinhTinDungTab)) CreateNewThamDinhTinDungTab();
            ChangeActiveTab(WindowsManager.GetInstance().TabThamDinhTinDung);
        }
        private void CreateNewThamDinhTinDungTab()
        {
            if (WindowsManager.GetInstance().TabThamDinhTinDung == null) WindowsManager.GetInstance().TabThamDinhTinDung = new ThamDinhTinDungTab(hoSoBindingSource);
            BaseTab tThamDinhTinDung = WindowsManager.GetInstance().TabThamDinhTinDung;
            mainTabs.TabPages.Add(tabThamDinhTinDungPage);
            Tabs.Add(TabType.ThamDinhTinDungTab);
            tThamDinhTinDung.LoadData();
            tabThamDinhHoSoData_Click(null, null);
        }
        private void tabThamDinhHoSoData_Click(object sender, EventArgs e)
        {
            if (this.tabThamDinhHoSoData.CurrentRow != null)
            {
                WindowsManager.GetInstance().TabThamDinhTinDung.SelectedIndex = this.tabThamDinhHoSoData.CurrentRow.Index;
            }
            else
            {
                WindowsManager.GetInstance().TabThamDinhTinDung.SelectedIndex = -1;
            }
        }
        #endregion

        #region Tham Dinh Tai San Dam Bao
        private void menuTDTSDBTatCaHoSo_Click(object sender, EventArgs e)
        {
            if (!IsContain(TabType.ThamDinhTaiSanDamBaoTab)) CreateNewThamDinhTaiSanDamBaoTab();
            ChangeActiveTab(WindowsManager.GetInstance().TabThamDinhTaiSanDamBao);
        }
        private void CreateNewThamDinhTaiSanDamBaoTab()
        {
            if (WindowsManager.GetInstance().TabThamDinhTaiSanDamBao == null) WindowsManager.GetInstance().TabThamDinhTaiSanDamBao = new ThamDinhTaiSanDamBaoTab(hoSoBindingSource);
            BaseTab tThamDinhTaiSanDamBao = WindowsManager.GetInstance().TabThamDinhTaiSanDamBao;
            mainTabs.TabPages.Add(tabThamDinhTaiSanDamBaoPage);
            Tabs.Add(TabType.ThamDinhTaiSanDamBaoTab);
            tThamDinhTaiSanDamBao.LoadData();
            if (this.tabThamDinhTSDBdata.SelectedCells.Count > 0) WindowsManager.GetInstance().TabThamDinhTaiSanDamBao.SelectedIndex = this.tabThamDinhTSDBdata.SelectedCells[0].RowIndex;
            else WindowsManager.GetInstance().TabThamDinhTaiSanDamBao.SelectedIndex = -1;
        }
        private void tabThamDinhTSDBdata_Click(object sender, EventArgs e)
        {
            if (this.tabThamDinhTSDBdata.SelectedCells.Count > 0)
            {
                WindowsManager.GetInstance().TabThamDinhTaiSanDamBao.SelectedIndex = this.tabThamDinhTSDBdata.SelectedCells[0].RowIndex;
            }
            else
            {
                WindowsManager.GetInstance().TabThamDinhTaiSanDamBao.SelectedIndex = -1;
            }
        }
        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (UserManager.GetInstance().IsAdmin())
            {
                menuNhanVien_Click(menuNhanVien, new EventArgs());
            }
            else if (UserManager.GetInstance().IsCBQHKH())
            {
                menuTatCaHoSo_Click(menuTiepNhanHoSo, new EventArgs());
            }
            else if (UserManager.GetInstance().IsCBTDTD())
            {
                menuTDTDTatCaHoSo_Click(menuTDTDTatCaHoSo, new EventArgs());
            }
            else if (UserManager.GetInstance().IsCBHTQHKH())
            {
                menuTDTSDBTatCaHoSo_Click(menuTDTSDBTatCaHoSo, new EventArgs());
            }
        }

        Color QM_MOUSE_HOVER_COLOR = Color.LightCyan;
        Color QM_MOUSE_LEAVE_COLOR = Color.White;
        private void qMenuTiepNhan_MouseHover(object sender, EventArgs e)
        {
            qMenuTiepNhanHS.BackColor = QM_MOUSE_HOVER_COLOR;
        }

        private void qMenuTiepNhan_MouseLeave(object sender, EventArgs e)
        {
            qMenuTiepNhanHS.BackColor = QM_MOUSE_LEAVE_COLOR;
        }

        private void qMenuTiepNhanHSMoi_MouseHover(object sender, EventArgs e)
        {
            qMenuTiepNhanHSMoi.BackColor = QM_MOUSE_HOVER_COLOR;
        }

        private void qMenuTiepNhanHSMoi_MouseLeave(object sender, EventArgs e)
        {
            qMenuTiepNhanHSMoi.BackColor = QM_MOUSE_LEAVE_COLOR;
        }

        private void qMenuKhachHang_MouseHover(object sender, EventArgs e)
        {
            qMenuKhachHang.BackColor = QM_MOUSE_HOVER_COLOR;
        }

        private void qMenuKhachHang_MouseLeave(object sender, EventArgs e)
        {
            qMenuKhachHang.BackColor = QM_MOUSE_LEAVE_COLOR;
        }

        private void qMenuThamDinhTD_MouseHover(object sender, EventArgs e)
        {
            qMenuThamDinhTD.BackColor = QM_MOUSE_HOVER_COLOR;
        }

        private void qMenuThamDinhTD_MouseLeave(object sender, EventArgs e)
        {
            qMenuThamDinhTD.BackColor = QM_MOUSE_LEAVE_COLOR;
        }

        private void qMenuThamDinhTSDB_MouseHover(object sender, EventArgs e)
        {
            qMenuThamDinhTSDB.BackColor = QM_MOUSE_HOVER_COLOR;
        }

        private void qMenuThamDinhTSDB_MouseLeave(object sender, EventArgs e)
        {
            qMenuThamDinhTSDB.BackColor = QM_MOUSE_LEAVE_COLOR;
        }

        private void qMenuNhanVien_MouseHover(object sender, EventArgs e)
        {
            qMenuNhanVien.BackColor = QM_MOUSE_HOVER_COLOR;
        }

        private void qMenuNhanVien_MouseLeave(object sender, EventArgs e)
        {
            qMenuNhanVien.BackColor = QM_MOUSE_LEAVE_COLOR;
        }

        private void qMenuTiepNhanHS_Click(object sender, EventArgs e)
        {
            this.menuTatCaHoSo_Click(sender, e);
        }

        private void qMenuTiepNhanHSMoi_Click(object sender, EventArgs e)
        {
            this.menuHoSoMoi_Click(sender, e);
        }

        private void qMenuKhachHang_Click(object sender, EventArgs e)
        {
            this.menuKhachHang_Click(sender, e);
        }

        private void qMenuThamDinhTD_Click(object sender, EventArgs e)
        {
            this.menuTDTDTatCaHoSo_Click(sender, e);
        }

        private void qMenuThamDinhTSDB_Click(object sender, EventArgs e)
        {
            this.menuTDTSDBTatCaHoSo_Click(sender, e);
        }

        private void qMenuNhanVien_Click(object sender, EventArgs e)
        {
            this.menuNhanVien_Click(sender, e);
        }
    }
}
